{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function array in module numpy.core.multiarray:\n",
      "\n",
      "array(...)\n",
      "    array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)\n",
      "    \n",
      "    Create an array.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    object : array_like\n",
      "        An array, any object exposing the array interface, an object whose\n",
      "        __array__ method returns an array, or any (nested) sequence.\n",
      "    dtype : data-type, optional\n",
      "        The desired data-type for the array.  If not given, then the type will\n",
      "        be determined as the minimum type required to hold the objects in the\n",
      "        sequence.  This argument can only be used to 'upcast' the array.  For\n",
      "        downcasting, use the .astype(t) method.\n",
      "    copy : bool, optional\n",
      "        If true (default), then the object is copied.  Otherwise, a copy will\n",
      "        only be made if __array__ returns a copy, if obj is a nested sequence,\n",
      "        or if a copy is needed to satisfy any of the other requirements\n",
      "        (`dtype`, `order`, etc.).\n",
      "    order : {'K', 'A', 'C', 'F'}, optional\n",
      "        Specify the memory layout of the array. If object is not an array, the\n",
      "        newly created array will be in C order (row major) unless 'F' is\n",
      "        specified, in which case it will be in Fortran order (column major).\n",
      "        If object is an array the following holds.\n",
      "    \n",
      "        ===== ========= ===================================================\n",
      "        order  no copy                     copy=True\n",
      "        ===== ========= ===================================================\n",
      "        'K'   unchanged F & C order preserved, otherwise most similar order\n",
      "        'A'   unchanged F order if input is F and not C, otherwise C order\n",
      "        'C'   C order   C order\n",
      "        'F'   F order   F order\n",
      "        ===== ========= ===================================================\n",
      "    \n",
      "        When ``copy=False`` and a copy is made for other reasons, the result is\n",
      "        the same as if ``copy=True``, with some exceptions for `A`, see the\n",
      "        Notes section. The default order is 'K'.\n",
      "    subok : bool, optional\n",
      "        If True, then sub-classes will be passed-through, otherwise\n",
      "        the returned array will be forced to be a base-class array (default).\n",
      "    ndmin : int, optional\n",
      "        Specifies the minimum number of dimensions that the resulting\n",
      "        array should have.  Ones will be pre-pended to the shape as\n",
      "        needed to meet this requirement.\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    out : ndarray\n",
      "        An array object satisfying the specified requirements.\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    empty_like : Return an empty array with shape and type of input.\n",
      "    ones_like : Return an array of ones with shape and type of input.\n",
      "    zeros_like : Return an array of zeros with shape and type of input.\n",
      "    full_like : Return a new array with shape of input filled with value.\n",
      "    empty : Return a new uninitialized array.\n",
      "    ones : Return a new array setting values to one.\n",
      "    zeros : Return a new array setting values to zero.\n",
      "    full : Return a new array of given shape filled with value.\n",
      "    \n",
      "    \n",
      "    Notes\n",
      "    -----\n",
      "    When order is 'A' and `object` is an array in neither 'C' nor 'F' order,\n",
      "    and a copy is forced by a change in dtype, then the order of the result is\n",
      "    not necessarily 'C' as expected. This is likely a bug.\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> np.array([1, 2, 3])\n",
      "    array([1, 2, 3])\n",
      "    \n",
      "    Upcasting:\n",
      "    \n",
      "    >>> np.array([1, 2, 3.0])\n",
      "    array([ 1.,  2.,  3.])\n",
      "    \n",
      "    More than one dimension:\n",
      "    \n",
      "    >>> np.array([[1, 2], [3, 4]])\n",
      "    array([[1, 2],\n",
      "           [3, 4]])\n",
      "    \n",
      "    Minimum dimensions 2:\n",
      "    \n",
      "    >>> np.array([1, 2, 3], ndmin=2)\n",
      "    array([[1, 2, 3]])\n",
      "    \n",
      "    Type provided:\n",
      "    \n",
      "    >>> np.array([1, 2, 3], dtype=complex)\n",
      "    array([ 1.+0.j,  2.+0.j,  3.+0.j])\n",
      "    \n",
      "    Data-type consisting of more than one element:\n",
      "    \n",
      "    >>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])\n",
      "    >>> x['a']\n",
      "    array([1, 3])\n",
      "    \n",
      "    Creating an array from sub-classes:\n",
      "    \n",
      "    >>> np.array(np.mat('1 2; 3 4'))\n",
      "    array([[1, 2],\n",
      "           [3, 4]])\n",
      "    \n",
      "    >>> np.array(np.mat('1 2; 3 4'), subok=True)\n",
      "    matrix([[1, 2],\n",
      "            [3, 4]])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(np.array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# numpy主要包括：\n",
    "# 一个具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组，称为 ndarray(N-dimensional array object)\n",
    "# 用于对整组数据进行快速运算的标准数学函数:ufunc(universal function object)\n",
    "# 实用的线性代数、傅里叶变换和随机数生成函数。 \n",
    "# NumPy和稀疏矩阵的运算包Scipy配合使用更加方便。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ndarray的创建\n",
    "# array函数:接收一个普通的python序列，并将其转换为ndarray \n",
    "# zeros函数:创建指定长度或者形状的全零数组。 \n",
    "# ones函数:创建指定长度或者形状的全1数组。\n",
    "# empty函数:创建一个没有任何具体值的数组(准确地说是创建一些未初始化 的ndarray多维数组)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  array函数\n",
    "list1= [1,2,3,4,5]\n",
    "np.array(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 二维\n",
    "list2=[[1,2,3],[4,5,6]]\n",
    "np.array(list2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用元祖\n",
    "tup1 = (1,2,3,4)\n",
    "np.array(tup1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 二维元祖\n",
    "tup2=((1,2,3),(4,5,6))\n",
    "np.array(tup2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# zeros创建\n",
    "np.zeros((3,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ones创建\n",
    "np.ones((3,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# empty创建\n",
    "np.empty((3,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 其他创建方式\n",
    "# arange函数: 类似python的range函数，通过指定开始值、终值和步长来 创建一个一维数组，注意:最终创建的数组不包含终值\n",
    "# linspace函数:通过指定开始值、终值和元素个数来创建一个一维数组， 数组的数据元素符合等差数列，可以通过endpoint关键字指定是否包含终 值，默认包含终值\n",
    "# logspace函数:和linspace函数类似，不过创建的是等比数列数组\n",
    "# 使用随机数填充数组，即使用numpy.random中的random()函数来创建 0-1之间的随机元素，数组包含的元素数量由参数决定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "       17, 18, 19])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# arange函数\n",
    "np.arange(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([15, 16, 17, 18, 19])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(15,20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  4,  7, 10, 13, 16, 19])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(1,20,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([20, 19, 18, 17, 16, 15, 14, 13, 12, 11])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(20,10,-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0. ,  2.5,  5. ,  7.5, 10. ])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#linspace函数\n",
    "np.linspace(0,10,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1.        ,   3.16227766,  10.        ,  31.6227766 ,\n",
       "       100.        ])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 0 10的0次方，2 10的2次方 生产5个\n",
    "np.logspace(0,2,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0.50787442, 0.31044095, 0.68875813],\n",
       "        [0.28016029, 0.71002687, 0.29759545]],\n",
       "\n",
       "       [[0.55898111, 0.52885932, 0.07378515],\n",
       "        [0.27973493, 0.44214567, 0.73361401]],\n",
       "\n",
       "       [[0.03133649, 0.63212325, 0.55014522],\n",
       "        [0.00160637, 0.97410845, 0.36918798]]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.random((3,2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.9567607 , -1.03743933, -1.77948851],\n",
       "       [-0.78235948, -1.46926069,  0.96612352],\n",
       "       [ 0.68582149,  0.71226067, -1.57550477]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标准正态分布的\n",
    "np.random.randn(3,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.30226687, 0.31859367, 0.71225271, 0.99191141, 0.67679263])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 5个0-1的随机数\n",
    "np.random.rand(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 5, 7],\n",
       "       [3, 8, 6],\n",
       "       [7, 2, 5]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 0-10的随机\n",
    "np.random.randint(1,10,(3,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[5, 2, 9, 9],\n",
       "        [9, 9, 8, 5],\n",
       "        [4, 1, 2, 9]],\n",
       "\n",
       "       [[7, 9, 7, 2],\n",
       "        [6, 7, 8, 2],\n",
       "        [9, 5, 7, 9]]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用reshape修改形状\n",
    "arr = np.random.randint(1,10,(2,3,4))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 2, 9, 9],\n",
       "       [9, 9, 8, 5],\n",
       "       [4, 1, 2, 9],\n",
       "       [7, 9, 7, 2],\n",
       "       [6, 7, 8, 2],\n",
       "       [9, 5, 7, 9]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = arr.reshape(6,4)\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 2, 9, 9, 9, 9],\n",
       "       [8, 5, 4, 1, 2, 9],\n",
       "       [7, 9, 7, 2, 6, 7],\n",
       "       [8, 2, 9, 5, 7, 9]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# -1表示自动计算\n",
    "arr2 = arr.reshape(-1,6)\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function where in module numpy.core.multiarray:\n",
      "\n",
      "where(...)\n",
      "    where(condition, [x, y])\n",
      "    \n",
      "    Return elements, either from `x` or `y`, depending on `condition`.\n",
      "    \n",
      "    If only `condition` is given, return ``condition.nonzero()``.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    condition : array_like, bool\n",
      "        When True, yield `x`, otherwise yield `y`.\n",
      "    x, y : array_like, optional\n",
      "        Values from which to choose. `x`, `y` and `condition` need to be\n",
      "        broadcastable to some shape.\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    out : ndarray or tuple of ndarrays\n",
      "        If both `x` and `y` are specified, the output array contains\n",
      "        elements of `x` where `condition` is True, and elements from\n",
      "        `y` elsewhere.\n",
      "    \n",
      "        If only `condition` is given, return the tuple\n",
      "        ``condition.nonzero()``, the indices where `condition` is True.\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    nonzero, choose\n",
      "    \n",
      "    Notes\n",
      "    -----\n",
      "    If `x` and `y` are given and input arrays are 1-D, `where` is\n",
      "    equivalent to::\n",
      "    \n",
      "        [xv if c else yv for (c,xv,yv) in zip(condition,x,y)]\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> np.where([[True, False], [True, True]],\n",
      "    ...          [[1, 2], [3, 4]],\n",
      "    ...          [[9, 8], [7, 6]])\n",
      "    array([[1, 8],\n",
      "           [3, 4]])\n",
      "    \n",
      "    >>> np.where([[0, 1], [1, 0]])\n",
      "    (array([0, 1]), array([1, 0]))\n",
      "    \n",
      "    >>> x = np.arange(9.).reshape(3, 3)\n",
      "    >>> np.where( x > 5 )\n",
      "    (array([2, 2, 2]), array([0, 1, 2]))\n",
      "    >>> x[np.where( x > 3.0 )]               # Note: result is 1D.\n",
      "    array([ 4.,  5.,  6.,  7.,  8.])\n",
      "    >>> np.where(x < 5, x, -1)               # Note: broadcasting.\n",
      "    array([[ 0.,  1.,  2.],\n",
      "           [ 3.,  4., -1.],\n",
      "           [-1., -1., -1.]])\n",
      "    \n",
      "    Find the indices of elements of `x` that are in `goodvalues`.\n",
      "    \n",
      "    >>> goodvalues = [3, 4, 7]\n",
      "    >>> ix = np.isin(x, goodvalues)\n",
      "    >>> ix\n",
      "    array([[False, False, False],\n",
      "           [ True,  True, False],\n",
      "           [False,  True, False]])\n",
      "    >>> np.where(ix)\n",
      "    (array([1, 1, 2]), array([0, 1, 1]))\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(np.where)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
